package com.sap.mobile.maf.samples.android.mafprovisioningsample;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SampleTabContent3 extends Activity {

	Bitmap mImageBitmap;
	ImageView mImageView;
	String mCurrentPhotoPath;
	Uri muri;
	LinearLayout ll;

	String filename = "rece";
	String photopath;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ll  = new LinearLayout(this);
		
		Button btn = new Button(this);
		btn.setId("get".hashCode());
		btn.setText("Take picture");
		btn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				takePhoto();
			}
		});
		
		mImageView = new ImageView(this);
		ll.addView(btn);
		ll.addView(mImageView);
		setContentView(ll);
			
		
	}

	private static final int TAKE_PHOTO_CODE = 1;
	public static final String IMAGE_UNSPECIFIED = "image/*"; 

	private void takePhoto(){
	  final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
	//  intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);  
	    
	  intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(this)) ); 
	  startActivityForResult(intent, TAKE_PHOTO_CODE);
	}

	private File getTempFile(Context context){
	  //it will return /sdcard/image.tmp
	  final File path = new File( Environment.getExternalStorageDirectory(), context.getPackageName() );
	  if(!path.exists()){
	    path.mkdir();
	  }
	  return new File(path, "image.tmp");
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	  if (resultCode == RESULT_OK) {
	    switch(requestCode){
	      case TAKE_PHOTO_CODE:
	        final File file = getTempFile(this);
	        try {
	          mImageBitmap = Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
	          mImageView.setImageBitmap(mImageBitmap);
	        } catch (FileNotFoundException e) {
	          e.printStackTrace();
	        } catch (IOException e) {
	          e.printStackTrace();
	        }
	      break;
	    }
	  }
	}
	private File createImageFile() throws IOException {
	    // Create an image file name
	    String timeStamp = 
	        new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
	    String imageFileName = "rece" + timeStamp + "_";
	    File image = File.createTempFile(
	        imageFileName, 
	        "rece"
	    );
	    mCurrentPhotoPath = image.getAbsolutePath();
	    return image;
	}
	
	
	

	


}